import { LabEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { normal_carry_max } from "utils/constants/definetypes";

export class LabUtils
{
    public static  isReactionReady(currentReactant:LabReaction,labModuel:LabEntity):boolean 
    {
        const baseLabs = labModuel.BaseLabs();
        const laba = baseLabs[0];
        const labb = baseLabs[1];
        const isvalidBaseLab = (lab:StructureLab,reactant:LabTarget)=>
        {
            if( lab.mineralType)
            {
                return lab.mineralType == reactant.target
            }
            return true;
        }
        if(isvalidBaseLab(laba,currentReactant.BaseA) && isvalidBaseLab(labb,currentReactant.BaseB))
        {
            return true;
        }
        return false;
    }
    public static  isBaseLabReady(currentReactant:LabReaction,labModuel:LabEntity):boolean 
    {
        const baseLabs = labModuel.BaseLabs();
        const laba = baseLabs[0];
        const labb = baseLabs[1];
        const isvalidBaseLab = (lab:StructureLab,reactant:LabTarget,amount?:number)=>
        {
            
            if(
                lab.mineralType == reactant.target )
            {
                 
                if(amount)
                {
                     
                    return lab.store.getUsedCapacity(lab.mineralType)>=(reactant.number-amount)
                }
                else 
                {
                    return lab.store.getUsedCapacity(lab.mineralType)>=reactant.number 
                }
            }
            return false;
        }
        if(isvalidBaseLab(laba,currentReactant.BaseA,currentReactant.Reactant.amount) && isvalidBaseLab(labb,currentReactant.BaseB,currentReactant.Reactant.amount))
        {
            return true;
        }
     
        return false;
    }
    /**
     * lab是否是干净的 true:干净 false:不干净 则需要清理
     * @param currentReactant 
     * @param reactionlab 
     * @returns 
     */
    public static  isCleanLab(currentReactant:LabReaction,reactionlab:LabEntity):boolean
    {
        const baseLabs = reactionlab.BaseLabs();
        const laba = baseLabs[0];
        const labb = baseLabs[1];
        const isvalidBaseLab = (lab:StructureLab,reactant:ResourceConstant)=>
        {
                if(  lab.mineralType &&  lab.mineralType != reactant)
                {
                    const amount = lab.store.getUsedCapacity<MineralConstant|MineralCompoundConstant>();
                    // 有材料但不是我要的类型

                    return false;
                }
                return true;
        }
        // 1.判断底物
        if(!isvalidBaseLab(laba,currentReactant.BaseA.target))
        {
             reactionlab.log('warn',`IsLabClean 底物A不符合->${JSON.stringify(currentReactant.BaseA.target)}`);
                return false;
        }
        if(!isvalidBaseLab(labb,currentReactant.BaseB.target))
        {
             reactionlab.log('warn',`IsLabClean 底物B不符合->${JSON.stringify(currentReactant.BaseA.target)}`);
                return false;
        }

        // 判断反应lab
        // 
        let minerial_sum=0;
        for(const lab of reactionlab.ReactionLabs())
        {
            if(lab.mineralType  )
            {
                if(lab.mineralType !=currentReactant.Reactant.target)
                {
                    reactionlab.log('warn',`IsLabClean 反应Lab不符合->${JSON.stringify(currentReactant.Reactant.target)}`);
                    return false;
                }
                if(lab.store.getUsedCapacity(lab.mineralType )>normal_carry_max)
                {
                    // 反应炉满了
                    return false;
                }
                minerial_sum+=lab.store.getUsedCapacity(lab.mineralType );
            }
        }
        if(minerial_sum>normal_carry_max)
        {
            // lab里存的资源过多了
            return false
        }
        return true;
    }

    public static isLabsEmpty(reactionlab:LabEntity):boolean
    {
        for(const lab of reactionlab.ReactionLabs())
        {
            if(lab.mineralType  )
            {
                return false;
            }
        }
        return true;
    }

    public static isReactantOver(currentReactant:LabReaction,labModuel:LabEntity):boolean
    {
        const baseLabs = labModuel.BaseLabs();
        const laba = baseLabs[0];
        const labb = baseLabs[1];
        if(laba && labb)
        {
            if(!laba.mineralType && !labb.mineralType)
            {
                return true;
            }
        }
        if(currentReactant.Reactant.amount && currentReactant.Reactant.amount>=currentReactant.Reactant.number)
        {
            return true;
        }
        return false;
    }
}